/// 加载状态枚举
enum LoadState {
  initial, // 初始状态
  loading, // 加载中
  loaded, // 加载完成
  error, // 错误状态
  empty, // 空数据
  loadMore, // 加载更多中
}

/// 基础异步状态
abstract class BaseAsyncState<T> {
  final LoadState state;
  final T? data;
  final String? error;
  final bool isRefreshing;

  const BaseAsyncState({
    required this.state,
    this.data,
    this.error,
    this.isRefreshing = false,
  });

  /// 是否正在加载
  bool get isLoading => state == LoadState.loading;

  /// 是否已加载
  bool get isLoaded => state == LoadState.loaded;

  /// 是否有错误
  bool get hasError => state == LoadState.error;

  /// 是否为空
  bool get isEmpty => state == LoadState.empty;

  /// 是否有数据
  bool get hasData => data != null;

  /// 是否正在加载更多
  bool get isLoadingMore => state == LoadState.loadMore;

  @override
  List<Object?> get props => [state, data, error, isRefreshing];
}

/// 列表状态
class ListState<T> extends BaseAsyncState<List<T>> {
  final bool hasMore;
  final int page;

  const ListState({
    required LoadState state,
    List<T>? data,
    String? error,
    bool isRefreshing = false,
    this.hasMore = true,
    this.page = 1,
  }) : super(
          state: state,
          data: data,
          error: error,
          isRefreshing: isRefreshing,
        );

  /// 初始状态
  const ListState.initial()
      : hasMore = true,
        page = 1,
        super(state: LoadState.initial);

  /// 加载中状态
  const ListState.loading({List<T>? data})
      : hasMore = true,
        page = 1,
        super(state: LoadState.loading, data: data);

  /// 加载完成状态
  ListState.loaded({
    required List<T> data,
    this.hasMore = true,
    this.page = 1,
  }) : super(
            state: data.isEmpty ? LoadState.empty : LoadState.loaded,
            data: data);

  /// 错误状态
  const ListState.error({
    required String error,
    List<T>? data,
  })  : hasMore = false,
        page = 1,
        super(state: LoadState.error, error: error, data: data);

  /// 刷新中状态
  const ListState.refreshing({List<T>? data})
      : hasMore = true,
        page = 1,
        super(state: LoadState.loading, data: data, isRefreshing: true);

  /// 加载更多中状态
  const ListState.loadingMore({
    required List<T> data,
    required this.page,
  })  : hasMore = true,
        super(state: LoadState.loadMore, data: data);

  /// 复制并更新
  ListState<T> copyWith({
    LoadState? state,
    List<T>? data,
    String? error,
    bool? isRefreshing,
    bool? hasMore,
    int? page,
    bool clearError = false,
  }) {
    return ListState<T>(
      state: state ?? this.state,
      data: data ?? this.data,
      error: clearError ? null : (error ?? this.error),
      isRefreshing: isRefreshing ?? this.isRefreshing,
      hasMore: hasMore ?? this.hasMore,
      page: page ?? this.page,
    );
  }

  @override
  List<Object?> get props => [...super.props, hasMore, page];
}

/// 详情状态
class DetailState<T> extends BaseAsyncState<T> {
  const DetailState({
    required LoadState state,
    T? data,
    String? error,
    bool isRefreshing = false,
  }) : super(
          state: state,
          data: data,
          error: error,
          isRefreshing: isRefreshing,
        );

  /// 初始状态
  const DetailState.initial() : super(state: LoadState.initial);

  /// 加载中状态
  const DetailState.loading({T? data})
      : super(state: LoadState.loading, data: data);

  /// 加载完成状态
  const DetailState.loaded({required T data})
      : super(state: LoadState.loaded, data: data);

  /// 错误状态
  const DetailState.error({
    required String error,
    T? data,
  }) : super(state: LoadState.error, error: error, data: data);

  /// 刷新中状态
  const DetailState.refreshing({T? data})
      : super(state: LoadState.loading, data: data, isRefreshing: true);

  /// 复制并更新
  DetailState<T> copyWith({
    LoadState? state,
    T? data,
    String? error,
    bool? isRefreshing,
    bool clearError = false,
  }) {
    return DetailState<T>(
      state: state ?? this.state,
      data: data ?? this.data,
      error: clearError ? null : (error ?? this.error),
      isRefreshing: isRefreshing ?? this.isRefreshing,
    );
  }
}

/// 分页响应数据结构
class PageResponse<T> {
  final List<T> data;
  final int page;
  final int totalPage;
  final int totalCount;
  final bool hasMore;

  PageResponse({
    required this.data,
    required this.page,
    required this.totalPage,
    required this.totalCount,
  }) : hasMore = page < totalPage;

  factory PageResponse.fromJson(
    Map<String, dynamic> json,
    T Function(Map<String, dynamic>) fromJson,
  ) {
    final dataList = json['data'] as List? ?? [];
    return PageResponse<T>(
      data: dataList
          .map((item) => fromJson(item as Map<String, dynamic>))
          .toList(),
      page: json['page'] ?? 1,
      totalPage: json['totalPage'] ?? 1,
      totalCount: json['totalCount'] ?? 0,
    );
  }
}
